<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['diy']=$this->plugin_setting('diy');
$return['express']=Array (
		'0' => Array ('id'=> 1, 'name' => '顺丰快递' ),
		'1' => Array ( 'id' => 2, 'name' => '申通快递' ) ,
		'2' => Array ( 'id' => 3,'name' => '韵达快运' ),
		'3' => Array ('id' => 4, 'name' => '天天快递' ),
		'4' => Array ( 'id' => 5, 'name' => '圆通速递' ) ,
		'5' => Array ( 'id' => 6,'name' => '中通速递' ) ,
		'6' => Array ( 'id' => 7 ,'name' => 'ems快递' ),
		'7' => Array ( 'id' => 8, 'name' => '汇通快运' ),
		'8' => Array ( 'id' => 9, 'name' => '其它快递' )
);
if($_RQ['op']=='main'){
	$merchid=intval($_RQ['merchid']);
	if($this->setting['zc_merch']){
		$merchid=model('jy_merch/model')->checkmerch($merchid);
	}else{
		$merchid=model('jy_merch/model')->checkadd_merch('','',$merchid);
	}
	$return['params']=array('merchid'=>$merchid,'status'=>10);
    $return['list']=array();
	$return['post']=array('id'=>0);
	if(!$merchid){
		exi('请选择商户！','error');
	}
	set_html('title','商户后台');
	exi($return);
}
/*******op*******/
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>intval($_RQ['merchid']));
	$return['params']['merchid']=intval($_RQ['merchid']);
	if(isset($_RQ['status']) && intval($_RQ['status'])!=10){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=intval($_RQ['status']);
	}else{
		$return['params']['status']=10;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('jy_merch_game_order',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($list){
		$allitemgame=$this->allitemgame;
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['info']=pdo_get('jy_merch_game_item',array('id'=>$item['pid'],'uniacid'=>$_SESSION['uniacid']),array('title'));
			$item['merch']=pdo_get('jy_merch_user',array('id'=>$item['merchid'],'uniacid'=>$_SESSION['uniacid']),array('id','merchname','logo','pcd','address'));
			$item['merch']['pcd']=json_decode($item['merch']['pcd'],true);
			$item['sendtypetitle']=$this->sendtypes[$item['sendtype']];
			$item['statustitle']=$this->order_status[$item['status']];
			if($item['status']>=2){
				$item['express']=pdo_get('jy_merch_game_express',array('order_id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				$item['express']['express_name']=$return['express'][$item['express']['express_id']-1]['name'];
			}
			$item['goods']=json_decode($item['goods'],true);
			$item['address']=json_decode($item['address'],true);
			if($item['address'] && $item['address']['name']){
				//$item['address']=$item['address'];
			}else{
				$item['address']=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['addressid']));
			}
			if(!is_array($item['address']['pcd'])){
				$item['address']['pcd']=json_decode($item['address']['pcd'],true);
			}
			$item['typetitle']=$allitemgame[$item['gamename']]['title'];
			$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
			if($item['tihuo_id']){
				$item['tihuo']=pdo_get('jy_merch_tihuo',array('id'=>$item['tihuo_id'],'uniacid'=>$_SESSION['uniacid']));
			}else{
				$item['tihuo']=pdo_get('jy_merch_tihuo',array('merchid'=>$item['merchid'],'uniacid'=>$_SESSION['uniacid']));
			}
			$item['tihuo']['pcd']=json_decode($item['tihuo']['pcd'],true);
		}
	}
	$return['list']=$list;
	exi($return);
}
//订单详情
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择','error');
	}
	$allitemgame=$this->allitemgame;
	$order=pdo_get('jy_merch_game_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$order['canyuform']=json_decode($order['canyuform'],true);
	$order['diyform']=json_decode($order['diyform'],true);
	//用户信息
	$member=cfc('core')->member_show($order['mid']);
	//项目信息
	$item=pdo_get('jy_merch_game_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['pid']));	
	//自定义字段数据
	$order['sendtypetitle']=$item['sendtype']==1?'快递发货':$item['sendtype']==0?'无需物流':'自提点提货';
	$address=pdo_get('core_address',array('id'=>$orderinfo['addressid'],'uniacid'=>$_SESSION['uniacid']));
	$address['pcd']=json_decode($address['pcd'],true);
	$addressinfo=$address['pcd']['province'] . $address['pcd']['city'] . $address['pcd']['district'] . $address['pcd']['street'] . $address['address'];
	$order['address']=$addressinfo;
	$order['mobile']=$address['mobile']?:$order['mobile'];
	$order['name']=$address['name']?:$order['realname'];
	$order['sendtypetitle']=$this->sendtypes[$order['sendtype']];
	$order['statustitle']=$this->order_status[$order['status']];
	if($order['status']>=2){
		$order['express']=pdo_get('jy_merch_game_express',array('order_id'=>$order['id'],'uniacid'=>$_SESSION['uniacid']));
		$order['express']['express_name']=$return['express'][$order['express']['express_id']-1]['name'];
	}
	$order['typetitle']=$allitemgame[$order['gamename']]['title'];
	$order['createtime']=date('Y-m-d H:i:s', $order['createtime']);
	$order['sendtime']=date('Y-m-d H:i:s', $order['sendtime']);
	$order['paytime']=date('Y-m-d H:i:s', $order['paytime']);
	$return['order']=$order;
	$return['member']=$member;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='fa'){
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$post['express_id'] || !$post['expresssn']){
			exi('请填写快递信息','error');
	}
	$data=array(
			'order_id'=>$id,
			'uniacid'=>$_SESSION['uniacid'],
			'express_id'=>$post['express_id'],
			'expresssn'=>$post['expresssn'],
			'createtime'=>time(),
	);
	$status=pdo_getcolumn('jy_merch_game_order',array('id'=>$id),'status');
	if($status!=1){
			exi('订单不是未发货状态','error');
	}
	pdo_insert('jy_merch_game_express',$data);
	pdo_update('jy_merch_game_order',array('status'=>2),array('id'=>$id));
	exi('已发货！','','main');
}
if($_RQ['op']=='receive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_update('jy_merch_game_order',array('status'=>'3'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$this->model('commission')->order_complete($order);
	exi('确认成功');
}
if($_RQ['op']=='pay'){
	$id = intval($_RQ['id']);
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$order=pdo_get('jy_merch_game_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	exi($order['pay_id']);
}
if($_RQ['op']=='del'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_delete('jy_merch_game_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除成功');
}